package com.example.antafter.wechat;

import com.alibaba.fastjson.JSONObject;
import com.example.antafter.entity.Weather;
import com.example.antafter.enums.AreaEnum;
import com.example.antafter.enums.UserEnum;
import com.example.antafter.utils.CaiHongPiUtil;
import com.example.antafter.utils.JiNianRiUtil;
import com.example.antafter.utils.WeatherUtil;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @authoer:WangMengqiang
 * @createDate:2022/9/15
 * @description:
 */
public class Pusher {

    public static void pushMsg() throws Exception {
        List<UserEnum> userList = getUsers();
        Map<UserEnum, Boolean> resultMap = new HashMap<>();
        for (UserEnum user : userList) {
            resultMap.put(user, packageMsg(user.getUserId()));
        }
        for (Map.Entry<UserEnum, Boolean> map : resultMap.entrySet()){
            String result = map.getValue() ? "成功" : "失败" ;
            System.out.println("推送至 " + map.getKey().getName() + "：" + result);
        }
    }

    private static boolean packageMsg(String userId) throws Exception {
        //1,配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId("wxbe0663e1506f4d1a");// appid
        wxStorage.setSecret("4717186dc0367aa51f17ba2e9e264e00");//appsecret
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(userId) // m
                .templateId("24WUhV8O83JOEPkjAGP2D3v7R26BJ939bu6LEnXvg5Q")// 模板id
//                .url("https://www.baidu.com")// 点击模板消息要访问的网址
                .build();
        //3,如果是正式版发送消息，，这里需要配置你的信息
        Weather weather = WeatherUtil.getWeather(AreaEnum.NANJING_YUHUATAI.getAreaCode());
        //String caiHongPi = CaiHongPiUtil.getCaiHongPi();
        Map<String, String> map = CaiHongPiUtil.getEnsentence();
        JSONObject lovePoems = CaiHongPiUtil.getLovePoems();
        String poem = lovePoems.getString("content") + "\n"
                + "------《" + lovePoems.getString("source") + "》"
                + " " + lovePoems.getString("author");
        // 日期
        templateMessage.addData(new WxMpTemplateData("riqi", weather.getDate() + "  "+ weather.getWeek() + "\n","#00BFFF"));
        // 天气
        templateMessage.addData(new WxMpTemplateData("tianqi",weather.getTextNow()+ "\n","#00FFFF"));
        // 最低温度
        templateMessage.addData(new WxMpTemplateData("low",weather.getLow() + "度" + "\n","#173177"));
        // 最高温度
        templateMessage.addData(new WxMpTemplateData("high",weather.getHigh() + "度" + "\n","#FF6347" ));
        // 相恋天数
        templateMessage.addData(new WxMpTemplateData("lianai", JiNianRiUtil.getLianAi() + "天" + "\n","#FF1493"));
        // 距下次生日天数
        templateMessage.addData(new WxMpTemplateData("birthday",JiNianRiUtil.getLunarBirthdayDays(1,24,false)+ "天" + "\n","#FFA500"));
        // 彩虹屁
        //templateMessage.addData(new WxMpTemplateData("caihongpi","\n" + caiHongPi + "\n\n","#C71585"));
        //情诗
        templateMessage.addData(new WxMpTemplateData("caihongpi","\n" + poem + "\n\n","#C71585"));
        // 每日英语
        templateMessage.addData(new WxMpTemplateData("english","\n" + map.get("en") + "\n","#C71585"));
        // 英语翻译
        templateMessage.addData(new WxMpTemplateData("chinese","\n" + map.get("zh") + "\n","#C71585"));
        // 备注 对生日及恋爱周年日进行提醒
        String beizhu = "❤";
        if(JiNianRiUtil.getLianAi() % 365 == 0){
            beizhu = "今天是恋爱" + (JiNianRiUtil.getLianAi() / 365) + "周年纪念日！";
        }
        if(JiNianRiUtil.getLunarBirthdayDays(1,24,false)  == 0){
            beizhu = "今天是宝贝生日，生日快乐呀！";
        }
        templateMessage.addData(new WxMpTemplateData("beizhu",beizhu + "\n","#FF0000"));
        try {
            String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            System.out.println("推送成功:" + msg);
            return true;
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }
        return false;
    }

    private static List<UserEnum> getUsers(){
        List<UserEnum> userList = new ArrayList<>();
//        for (UserEnum user : UserEnum.values()){
//            userList.add(user);
//        }

        userList.add(UserEnum.MAHAHA);
        return userList;
    }


    public static void main(String[] args) throws Exception {
        pushMsg();
    }
}